package in.muton.edushare.components.calendar;

import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.components.calendar.api.CalendarInterface;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.schedules.widgets.DateHelper;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class CalendarPanel extends EdusharePanel {

	public CalendarPanel(String id, Users user, EduShareBasePage page,
			int month, int year) {
		super(id, page);
		authorisation = (user == getLoggedInUser());
		scheduleList = new ScheduleModel(user);
		add(new EvtDialog("eventDialog", user, page));
		init(month, year);
	}

	public CalendarPanel(String id, Users user, EduShareBasePage page) {
		this(id, user, page, -1, -1);
	}

	public CalendarPanel(String id, Departments dept, EduShareBasePage page,
			int month, int year) {
		super(id, page);
		scheduleList = new ScheduleModel(dept);
		Authentication auth = new Authentication(dept, getLoggedInUser());
		authorisation = auth.isAllowed(Authentication.CAL_NEW);
		add(new EvtDialog("eventDialog", dept, page));
		init(month, year);
	}

	public CalendarPanel(String id, Departments dept, EduShareBasePage page) {
		this(id, dept, page, -1, -1);
	}

	public CalendarPanel(String id, Groups group, EduShareBasePage page, int month, int year) {
		super(id, page);
		scheduleList = new ScheduleModel(group);
		Authentication auth = new Authentication(group, getLoggedInUser());
		authorisation = auth.isAllowed(Authentication.CAL_NEW);
		add(new EvtDialog("eventDialog", group, page));
		init(month, year);
	}

	public CalendarPanel(String id, Groups group, EduShareBasePage page) {
		this(id, group, page, -1, -1);
	}

	public CalendarPanel(String id, CourseInstances course,
			EduShareBasePage page, int month, int year) {
		super(id, page);
		Authentication auth = new Authentication(course, getLoggedInUser());
		authorisation = auth.isAllowed(Authentication.CAL_NEW);
		scheduleList = new ScheduleModel(course);
		add(new EvtDialog("eventDialog", course, page));
		init(month, year);
	}

	public CalendarPanel(String id, CourseInstances course,
			EduShareBasePage page) {
		this(id, course, page, -1, -1);
	}

	public CalendarPanel(String id, Universities univ, EduShareBasePage page,
			int month, int year) {
		super(id, page);
		scheduleList = new ScheduleModel(univ);
		Authentication auth = new Authentication(univ, getLoggedInUser());
		authorisation = auth.isAllowed(Authentication.CAL_NEW);
		add(new EvtDialog("eventDialog", univ, page));
		init(month, year);
	}

	public CalendarPanel(String id, Universities univ, EduShareBasePage page) {
		this(id, univ, page, -1, -1);
	}

	private void init(int month, int year) {
		add(CSSPackageResource.getHeaderContribution(CalendarPanel.class,
				"Calendar.css"));
		this.date = new Date();
		DateHelper d = new DateHelper(date);
		if(month != -1){
			d.setMonths(month);
			d.setYears(year);
		}
		
		add(new Link<String>("prevmonth") {
			private static final long serialVersionUID = 8014597569948272231L;

			public void onClick() {
				date = new DateHelper(date).addMonths(-1).getDate();
			}
		});
		
		add(new Link<String>("nextmonth") {
			private static final long serialVersionUID = 6774552657897887406L;

			public void onClick() {
				date = new DateHelper(date).addMonths(1).getDate();
			}
		});
		add(new Label("monthname", new Model<String>() {
			private static final long serialVersionUID = 3713581204493544702L;

			@Override
			public String getObject() {
				GregorianCalendar gc = new GregorianCalendar();
				gc.setTime(CalendarPanel.this.date);
				return new DateFormatSymbols().getMonths()[gc
						.get(Calendar.MONTH)]
						+ " " + gc.get(Calendar.YEAR);
			}
		}));

		add(new CalendarGridView("calendarTable", scheduleList, authorisation,
				new Model<Date>() {
					private static final long serialVersionUID = -4725074488767286273L;

					@Override
					public Date getObject() {
						return date;
					}

				}));
	}

	private class ScheduleModel implements IModel<CayenneList<Schedules>> {
		private static final long serialVersionUID = 2463624561073260837L;

		public ScheduleModel(Object obj) {
			this.obj = new CayenneModel<Object>(obj);
		}

		public CayenneList<Schedules> getObject() {
			if (obj.getObject() instanceof Universities) {
				return CalendarInterface.getSchedules((Universities) (obj
						.getObject()));
			}
			if (obj.getObject() instanceof Departments) {
				return CalendarInterface.getSchedules((Departments) (obj
						.getObject()));
			}
			if (obj.getObject() instanceof CourseInstances) {
				return CalendarInterface.getSchedules((CourseInstances) (obj
						.getObject()));
			}
			if (obj.getObject() instanceof Users) {
				return CalendarInterface
						.getSchedules((Users) (obj.getObject()));
			}
			if (obj.getObject() instanceof Groups) {
				return CalendarInterface
						.getSchedules((Groups) (obj.getObject()));
			}
			return new CayenneList<Schedules>(null);
		}

		public void setObject(CayenneList<Schedules> arg0) {
		}

		public void detach() {
		}

		private CayenneModel<Object> obj;
	}
	
	public String getMonth(){
		return new SimpleDateFormat("MMM, yyyy").format(date);
	}

	boolean authorisation = false;
	ScheduleModel scheduleList = null;
	List<Date> dayList = new ArrayList<Date>();
	private Date date = null;
	private static final long serialVersionUID = 4942865508693436711L;
}
